package com.mtiiot.fstcpserver.fastdfs.core;

import java.io.IOException;
import java.io.OutputStream;

public class DownloadStream
  implements DownloadCallback
{
  private OutputStream out;
  private long currentBytes = 0L;

  public DownloadStream(OutputStream out)
  {
    this.out = out;
  }

  public int recv(long fileSize, byte[] data, int bytes)
  {
    try
    {
      this.out.write(data, 0, bytes);
    }
    catch (IOException ex)
    {
      ex.printStackTrace();
      return -1;
    }

    this.currentBytes += bytes;
    if (this.currentBytes == fileSize)
    {
      this.currentBytes = 0L;
    }

    return 0;
  }
}